import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import { Modal } from "view-design"
import qs from "qs"

import Home from "../views/Home"
import Login from "../views/Login"
import Contact from "../views/Contact"
import Register from "../views/Register"

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    component: Home,
    meta: {title: '首页', isLogin: true}
  },
  {
    path: '/login',
    component: Login,
    meta: {title: '登录', isLogin: false}
  },
  {
    path: '/contact',
    component: Contact,
    meta: {title: '联系我们', isLogin: false}
  },
  {
    path: '/register',
    component: Register,
    meta: {title: '银行注册', isLogin: false}
  },
]

Vue.use(VueRouter)
const router = new VueRouter({
  // 配置路由和组件之间的映射关系
  routes,
  mode: 'history'
});

router.beforeEach((to, from, next) => {
  if (store.state.isLogin === true) {
    next()
  } else {
    let isLogin = qs.parse(sessionStorage.getItem('state')).isLogin === 'true'
    if (to.meta.isLogin && !isLogin) {
      Modal.confirm({
        title: '登录提示',
        content: '点击确定进行登录',
        onOk: function () {
          next({path: '/login'})
        },
        onCancel: function () {
          return false
        }
      })
    } else {
      next()
    }
  }
  if (to.meta.title) document.title = '数据保护与共享demo - ' + to.meta.title
  else document.title = '数据保护与共享demo'
});

router.afterEach(() => {
  window.scroll(0, 0)
});

export default router
